package com.newcapec.erp.ui;

import com.newcapec.erp.main.DataInit;
import com.newcapec.erp.utils.XMLFReader;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class SelectPanelUI {

    //获取仓库管理查询区域UI
    public static JPanel getWarehouseSelectPanel(){

        JPanel selectPanel = new JPanel(null);
        JLabel label_code = new JLabel("仓库代码：");
        label_code.setBounds(5,10,70,20);
        selectPanel.add(label_code);

        JTextField textField_code = new JTextField();
        textField_code.setBounds(80, 10, 70, 20);
        selectPanel.add(textField_code);
        textField_code.setColumns(10);

        JLabel label_name = new JLabel("仓库名：");
        label_name.setBounds(170,10,70,20);
        selectPanel.add(label_name);

        JTextField textField_name = new JTextField();
        textField_name.setBounds(245, 10, 70, 20);
        selectPanel.add(textField_name);
        textField_name.setColumns(10);

        JLabel label_attr = new JLabel("仓库属性：");
        label_attr.setBounds(335,10,70,20);
        selectPanel.add(label_attr);

        JTextField textField_attr = new JTextField();
        textField_attr.setColumns(10);
        textField_attr.setBounds(410, 10, 70, 20);
        selectPanel.add(textField_attr);


        JButton button = new JButton("\u67E5\u8BE2");
        button.setBounds(920, 10, 70, 20);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectCode = textField_code.getText();
                String selectName = textField_name.getText();
                String selectAttr = textField_attr.getText();
                System.out.println(selectCode+" "+selectName+" "+selectAttr);
                Vector<Vector<String>> selectWarehouses = getSelectWarehouses(selectCode,selectName,selectAttr);
                //根据查询更新界面
                MainUI.updateMainUI(DataInit.TYPE_WAREHOUSE,selectWarehouses,1);
            }
        });
        selectPanel.add(button);

        return selectPanel;
    }

    //获取仓库查询结果数据
    public static Vector<Vector<String>> getSelectWarehouses(String selectCode,String selectName,String selectAttr){
        Vector<Vector<String>> allWarehouses = XMLFReader.getData(DataInit.TYPE_WAREHOUSE);
        Vector<Vector<String>> selectWarehouses = new Vector<>();
        for (Vector<String> warehouse:allWarehouses){
            if(warehouse.get(0).contains(selectCode) && warehouse.get(1).contains(selectName) && warehouse.get(2).contains(selectAttr)){
                selectWarehouses.add(warehouse);
            }
        }
        return selectWarehouses;
    }

    //获取储位管理查询区域UI
    public static JPanel getStoreSelectPanel(){

        JPanel selectPanel = new JPanel(null);
        JLabel label_warehouse = new JLabel("仓库：");
        label_warehouse.setBounds(5,10,40,20);
        selectPanel.add(label_warehouse);

        JComboBox comboBox_warehouse = new JComboBox();
        comboBox_warehouse.setModel(new DefaultComboBoxModel(new String[] {"全部","1号库","2号库","3号库"}));
        comboBox_warehouse.setBounds(50, 10, 70, 20);
        selectPanel.add(comboBox_warehouse);

        JLabel label_client = new JLabel("所属客户：");
        label_client.setBounds(140,10,70,20);
        selectPanel.add(label_client);

        JTextField textField_client = new JTextField();
        textField_client.setBounds(215, 10, 70, 20);
        selectPanel.add(textField_client);
        textField_client.setColumns(10);

        JLabel label_type = new JLabel("储位类型：");
        label_type.setBounds(305,10,70,20);
        selectPanel.add(label_type);

        JTextField textField_type = new JTextField();
        textField_type.setColumns(10);
        textField_type.setBounds(380, 10, 70, 20);
        selectPanel.add(textField_type);

        JLabel label_attr = new JLabel("储位属性：");
        label_attr.setBounds(470,10,70,20);
        selectPanel.add(label_attr);

        JComboBox comboBox_attr = new JComboBox();
        comboBox_attr.setModel(new DefaultComboBoxModel(new String[] {"全部","保鲜","冷藏"}));
        comboBox_attr.setBounds(545, 10, 70, 20);
        selectPanel.add(comboBox_attr);

        JLabel label_product = new JLabel("产品属性：");
        label_product.setBounds(635,10,70,20);
        selectPanel.add(label_product);

        JTextField textField_product = new JTextField();
        textField_product.setBounds(710, 10, 70, 20);
        selectPanel.add(textField_product);
        textField_product.setColumns(10);

        JButton button = new JButton("\u67E5\u8BE2");
        button.setBounds(920, 10, 70, 20);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectWarehouse = comboBox_warehouse.getSelectedItem().toString();
                String selectClient = textField_client.getText();
                String selectType = textField_type.getText();
                String selectAttr = comboBox_attr.getSelectedItem().toString();
                String selectProduct = textField_product.getText();
                Vector<Vector<String>> selectStores = getSelectStores(selectWarehouse,selectClient,selectType,selectAttr,selectProduct);
                MainUI.updateMainUI(DataInit.TYPE_STORE,selectStores,1);
            }
        });
        selectPanel.add(button);

        return selectPanel;
    }

    //获取储位查询结果数据
    public static Vector<Vector<String>> getSelectStores(String selectWarehouse,String selectClient,String selectType,String selectAttr,String selectProduct){
        Vector<Vector<String>> allStores = XMLFReader.getData(DataInit.TYPE_STORE);
        Vector<Vector<String>> selectStores = new Vector<>();
        if(selectWarehouse.equals("全部")){
            selectWarehouse = "";
        }
        if(selectAttr.equals("全部")){
            selectAttr = "";
        }
        for (Vector<String> store:allStores){
            if(store.get(0).contains(selectWarehouse) && store.get(1).contains(selectClient) &&
                    store.get(2).contains(selectType) && store.get(3).contains(selectAttr) &&
                    store.get(4).contains(selectProduct)){
                selectStores.add(store);
            }
        }
        return selectStores;
    }

    //获取系统管理查询区域UI
    public static JPanel getSystemSelectPanel(){

        JPanel selectPanel = new JPanel(null);
        JLabel label_0 = new JLabel("用户账号：");
        label_0.setBounds(5,10,70,20);
        selectPanel.add(label_0);

        JTextField textField_0 = new JTextField();
        textField_0.setBounds(80, 10, 70, 20);
        selectPanel.add(textField_0);
        textField_0.setColumns(10);

        JLabel label_1 = new JLabel("用户名称：");
        label_1.setBounds(170,10,70,20);
        selectPanel.add(label_1);

        JTextField textField_1 = new JTextField();
        textField_1.setBounds(245, 10, 70, 20);
        selectPanel.add(textField_1);
        textField_1.setColumns(10);

        JLabel label_2 = new JLabel("组织机构：");
        label_2.setBounds(335,10,70,20);
        selectPanel.add(label_2);

        JTextField textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(410, 10, 70, 20);
        selectPanel.add(textField_2);

        JLabel label_3 = new JLabel("角色：");
        label_3.setBounds(500,10,70,20);
        selectPanel.add(label_3);

        JTextField textField_3 = new JTextField();
        textField_3.setBounds(575, 10, 70, 20);
        selectPanel.add(textField_3);
        textField_3.setColumns(10);

        JButton button = new JButton("\u67E5\u8BE2");
        button.setBounds(920, 10, 70, 20);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String select_0 = textField_0.getText();
                String select_1 = textField_1.getText();
                String select_2 = textField_2.getText();
                String select_3 = textField_3.getText();
                Vector<Vector<String>> selectSystem = getSelectSystem(select_0,select_1,select_2,select_3);
                MainUI.updateMainUI(DataInit.TYPE_SYSTEM,selectSystem,1);
            }
        });
        selectPanel.add(button);

        return selectPanel;
    }

    //获取系统查询结果数据
    public static Vector<Vector<String>> getSelectSystem(String select_0,String select_1,String select_2,String select_3){
        Vector<Vector<String>> allSystem = XMLFReader.getData(DataInit.TYPE_SYSTEM);
        Vector<Vector<String>> selectSystem = new Vector<>();
        for (Vector<String> system:allSystem){
            if(system.get(0).contains(select_0) && system.get(1).contains(select_1) &&
                    system.get(2).contains(select_2) && system.get(3).contains(select_3)){
                selectSystem.add(system);
            }
        }
        return selectSystem;
    }

    //获取客户管理查询区域UI
    public static JPanel getClientSelectPanel(){

        JPanel selectPanel = new JPanel(null);
        JLabel label_name = new JLabel("中文全称：");
        label_name.setBounds(5,10,70,20);
        selectPanel.add(label_name);

        JTextField textField_name = new JTextField();
        textField_name.setBounds(80, 10, 70, 20);
        selectPanel.add(textField_name);
        textField_name.setColumns(10);

        JLabel label_CN = new JLabel("合同号：");
        label_CN.setBounds(170,10,70,20);
        selectPanel.add(label_CN);

        JTextField textField_CN = new JTextField();
        textField_CN.setBounds(245, 10, 70, 20);
        selectPanel.add(textField_CN);
        textField_CN.setColumns(10);




        JButton button = new JButton("\u67E5\u8BE2");
        button.setBounds(920, 10, 70, 20);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectName = textField_name.getText();
                String selectCN = textField_CN.getText();

                System.out.println(selectName+" "+selectCN);
                Vector<Vector<String>> selectallClients = getClient(selectName,selectCN);
                //根据查询更新界面
                MainUI.updateMainUI(DataInit.TYPE_CLIENT,selectallClients,1);
            }
        });
        selectPanel.add(button);

        return selectPanel;
    }

    //获取客户管理查询结果数据
    public static Vector<Vector<String>> getClient(String selectName,String selectCN){
        Vector<Vector<String>> allClients = XMLFReader.getData(DataInit.TYPE_CLIENT);
        Vector<Vector<String>> selectallClients = new Vector<>();
        for (Vector<String> clients:allClients){
            if(clients.get(0).contains(selectName) && clients.get(1).contains(selectCN) ){
                selectallClients.add(clients);
            }
        }
        return selectallClients;
    }



    //获取库存查询区域UI
    public static JPanel getInventorySelectPanel(){

        JPanel selectPanel = new JPanel(null);
        JLabel label_code = new JLabel("商品编码：");
        label_code.setBounds(5,10,70,20);
        selectPanel.add(label_code);

        JTextField textField_code = new JTextField();
        textField_code.setBounds(80, 10, 70, 20);
        selectPanel.add(textField_code);
        textField_code.setColumns(10);

        JLabel label_name = new JLabel("商品名称：");
        label_name.setBounds(170,10,70,20);
        selectPanel.add(label_name);

        JTextField textField_name = new JTextField();
        textField_name.setBounds(245, 10, 70, 20);
        selectPanel.add(textField_name);
        textField_name.setColumns(10);

        JLabel label_client = new JLabel("客户：");
        label_client.setBounds(335,10,70,20);
        selectPanel.add(label_client);

        JTextField textField_client = new JTextField();
        textField_client.setColumns(10);
        textField_client.setBounds(410, 10, 70, 20);
        selectPanel.add(textField_client);


        JButton button = new JButton("\u67E5\u8BE2");
        button.setBounds(920, 10, 70, 20);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectCode = textField_code.getText();
                String selectName = textField_name.getText();
                String selectClient = textField_client.getText();
                System.out.println(selectCode+" "+selectName+" "+selectClient);
                Vector<Vector<String>> selectInventory = getInventory(selectCode,selectName,selectClient);
                //根据查询更新界面
                MainUI.updateMainUI(DataInit.TYPE_INVENTORY,selectInventory,1);
            }
        });
        selectPanel.add(button);

        return selectPanel;
    }


    //获取库存管理查询结果数据
    public static Vector<Vector<String>> getInventory(String selectCode,String selectName,String selectClient){
        Vector<Vector<String>> allInventory = XMLFReader.getData(DataInit.TYPE_INVENTORY);
        Vector<Vector<String>> selectInventory = new Vector<>();
        for (Vector<String> inventory:allInventory){
            if(inventory.get(1).contains(selectCode) && inventory.get(2).contains(selectName) && inventory.get(4).contains(selectClient)){
                selectInventory.add(inventory);
            }
        }
        return selectInventory;
    }

}
